﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Security.Cryptography;
using System.IO;

namespace AES
{
    class FileSplitter
    {
        private static void WriteFile(string fileName, byte[] data, int bytesCount , string password )
        {
            byte[] encryptedData =  EncryptionWithStreams.EncryptBytes( data , password);

            using (FileStream outFile = new FileStream(fileName, FileMode.Create))
            {
                outFile.Write(encryptedData, 0, bytesCount);
            }
        }

        private static void FileSplit(string inputFileName , int OutputFilesMaximumSize , string password)
        {
            using (FileStream inputFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read))
            {
                byte[] data = new byte[OutputFilesMaximumSize];
                int dataFreeIndex=0;
                int outputFileIndex = 0;

                while (true)
                {
                    int readBytes = inputFile.Read(data, dataFreeIndex, OutputFilesMaximumSize - dataFreeIndex);
                    dataFreeIndex += readBytes;

                    if (readBytes == 0 || dataFreeIndex == OutputFilesMaximumSize)
                    {
                        WriteFile( string.Format("{0}-part{1:D4}" ,inputFileName ,outputFileIndex) , data, dataFreeIndex, password );

                        dataFreeIndex = 0;
                        outputFileIndex++;
                    }

                    if (readBytes == 0)
                    {
                        break;
                    }
                }

            }
        }

        static void Main(string[] args)
        {
            FileSplit( @"C:\Temp\a.mpeg" , 1024*1024*10 , "parolka");
        }
    }
}
